package com.woniuxy.his.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniuxy.his.common.Result;
import com.woniuxy.his.controller.form.AddArrangeForm;
import com.woniuxy.his.entity.Department;
import com.woniuxy.his.entity.Doctor;
import com.woniuxy.his.mapper.DepartmentMapper;
import com.woniuxy.his.mapper.DoctorMapper;
import com.woniuxy.his.service.IArrangeService;
import com.woniuxy.his.vo.ArrangeVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;

/**
 * <p>
 * 排班表 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2021-08-24
 */
@RestController
//@RequestMapping("/his/arrange")
@Slf4j

public class ArrangeController {

    @Resource
    private IArrangeService arrangeService;

    @Resource
    private DepartmentMapper departmentMapper;

    @Resource
    private DoctorMapper doctorMapper;

    //取消排班
    @GetMapping("/delArrange")
    public Result delArrange(Integer arrangeId){
        log.info("排班表id为{}",arrangeId);
        Integer del = arrangeService.delArrange(arrangeId);

        if(del>0){
            return Result.success("200","删除成功",del);
        }
        return Result.fail("400","删除失败",del);
    }

    @GetMapping("/findAllArrange")
    public Result findAllArrange(String arrangeTime,Integer current,Integer size){
        log.info("排班时间为{},size{},current{}",arrangeTime,size,current);

        IPage arrangeByPage = arrangeService.findArrangeByPage(arrangeTime, current, size);
        if (null==arrangeByPage) {
            return Result.fail("400","未找到排班","");
        }else{
            log.info("找到排班信息并返回");
            return Result.success("200","找到排班信息",arrangeByPage);
        }
    }
    @GetMapping("/findAllArrange2")
    public Result findAllArrange2(Integer current,Integer size){
        log.info("排班时间为{},size{},current{}",size,current);

        IPage arrangeByPage = arrangeService.findArrangeByPage2(current, size);
        if (null==arrangeByPage) {
            return Result.fail("400","未找到排班","");
        }else{
            log.info("找到排班信息并返回");
            return Result.success("200","找到排班信息",arrangeByPage);
        }
    }


    @PostMapping("/addArrange")
    public Result addArrange(@RequestBody AddArrangeForm addArrangeForm) throws ParseException {
        log.info("添加排班数据为{}",addArrangeForm);
        Integer insert = arrangeService.addArrange(addArrangeForm);
        if(0<insert){
            return Result.success("200","排班成功",insert);
        }
        return Result.fail("400","排班失败",insert);
    }

    @PostMapping("/updateArrange")
    public Result updateArrange(@RequestBody AddArrangeForm addArrangeForm){
        log.info("修改排班数据为{}",addArrangeForm);
        Integer insert = arrangeService.updateArrange(addArrangeForm);
        if(0<insert){
            return Result.success("200","调整排班成功",insert);
        }
        return Result.fail("400","调整排班失败",insert);
    }

    @GetMapping("/findDepName")
    public Result findDepName(Integer depId){
        log.info("部门id得到为{}",depId);
        QueryWrapper<Department> wrapper = new QueryWrapper<>();
        wrapper.eq("dep_id",depId);
        Department department = departmentMapper.selectOne(wrapper);
        if(null!=department){
            return Result.success("200","成功找到部门名",department.getDepName());
        }
        return Result.fail("400","失败",null);
    }

    @GetMapping("/findDoctorName")
    public Result findDocorName(Integer doctorId){
        log.info("医生id为{}",doctorId);
        QueryWrapper<Doctor> wrapper = new QueryWrapper<>();
        wrapper.eq("doctor_id",doctorId);
        Doctor doctor = doctorMapper.selectOne(wrapper);
        if(null!=doctor){
            return Result.success("200","成功找到部门名",doctor.getDoctorName());
        }
        return Result.fail("400","失败",null);

    }

}

